//package cn.smbms.servlet.user;
//
//import cn.smbms.pojo.Role;
//import cn.smbms.pojo.User;
//import cn.smbms.service.role.RoleService;
//import cn.smbms.service.role.RoleServiceImpl;
//import cn.smbms.service.user.UserService;
//import cn.smbms.tools.Constants;
//import cn.smbms.tools.PageSupport;
//import com.alibaba.fastjson.JSONArray;
//import com.mysql.jdbc.StringUtils;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.support.ClassPathXmlApplicationContext;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//public class UserServlet extends HttpServlet {
//	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//	UserService userService=context.getBean("user", UserService.class);
//	/**
//	 * Constructor of the object.
//	 */
//	public UserServlet() {
//		super();
//	}
//
//	/**
//	 * Destruction of the servlet. <br>
//	 */
//	@Override
//	public void destroy() {
//		super.destroy(); // Just puts "destroy" string in log
//		// Put your code here
//	}
//
//	/**
//	 * The doGet method of the servlet. <br>
//	 *
//	 * This method is called when a form has its tag value method equals to get.
//	 *
//	 * @param request the request send by the client to the server
//	 * @param response the response send by the server to the client
//	 * @throws ServletException if an error occurred
//	 * @throws IOException if an error occurred
//	 */
//	@Override
//	public void doGet(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		doPost(request, response);
//	}
//
//	/**
//	 * The doPost method of the servlet. <br>
//	 *
//	 * This method is called when a form has its tag value method equals to post.
//	 *
//	 * @param request the request send by the client to the server
//	 * @param response the response send by the server to the client
//	 * @throws ServletException if an error occurred
//	 * @throws IOException if an error occurred
//	 */
//	@Override
//	public void doPost(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//
//		String method = request.getParameter("method");
//		System.out.println("method----> " + method);
//
//		if(method != null && method.equals("add")){
//			//增加操作
//			this.add(request, response);
//		}else if(method != null && method.equals("query")){
//			this.query(request, response);
//		}else if(method != null && method.equals("getrolelist")){
//			this.getRoleList(request, response);
//		}else if(method != null && method.equals("ucexist")){
//			this.userCodeExist(request, response);
//		}else if(method != null && method.equals("deluser")){
//			this.delUser(request, response);
//		}else if(method != null && method.equals("view")){
//			this.getUserById(request, response,"userview");
//		}else if(method != null && method.equals("modify")){
//			this.getUserById(request, response,"usermodify");
//		}else if(method != null && method.equals("modifyexe")){
//			this.modify(request, response);
//		}else if(method != null && method.equals("pwdmodify")){
//			this.getPwdByUserId(request, response);
//		}else if(method != null && method.equals("savepwd")){
//			this.updatePwd(request, response);
//		}
//
//	}
//
//
//	private void updatePwd(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//
//		Object o = request.getSession().getAttribute(Constants.USER_SESSION);
//		String newpassword = request.getParameter("newpassword");
//		boolean flag = false;
//		if(o != null && !StringUtils.isNullOrEmpty(newpassword)){
//			//UserService userService = new UserServiceImpl();
//			flag = userService.updatePwd(((User)o).getId(),newpassword);
//			if(flag){
//				request.setAttribute(Constants.SYS_MESSAGE, "修改密码成功,请退出并使用新密码重新登录！");
//				request.getSession().removeAttribute(Constants.USER_SESSION);//session注销
//			}else{
//				request.setAttribute(Constants.SYS_MESSAGE, "修改密码失败！");
//			}
//		}else{
//			request.setAttribute(Constants.SYS_MESSAGE, "修改密码失败！");
//		}
//		request.getRequestDispatcher("pwdmodify.jsp").forward(request, response);
//	}
//
//	private void getPwdByUserId(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		Object o = request.getSession().getAttribute(Constants.USER_SESSION);
//		String oldpassword = request.getParameter("oldpassword");
//		Map<String, String> resultMap = new HashMap<String, String>();
//
//		if(null == o ){//session过期
//			resultMap.put("result", "sessionerror");
//		}else if(StringUtils.isNullOrEmpty(oldpassword)){//旧密码输入为空
//			resultMap.put("result", "error");
//		}else{
//			String sessionPwd = ((User)o).getUserPassword();
//			if(oldpassword.equals(sessionPwd)){
//				resultMap.put("result", "true");
//			}else{//旧密码输入不正确
//				resultMap.put("result", "false");
//			}
//		}
//
//		response.setContentType("application/json");
//		PrintWriter outPrintWriter = response.getWriter();
//		outPrintWriter.write(JSONArray.toJSONString(resultMap));
//		outPrintWriter.flush();
//		outPrintWriter.close();
//	}
//
//
//	private void modify(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		String id = request.getParameter("uid");
//		String userName = request.getParameter("userName");
//		String gender = request.getParameter("gender");
//		String birthday = request.getParameter("birthday");
//		String phone = request.getParameter("phone");
//		String address = request.getParameter("address");
//		String userRole = request.getParameter("userRole");
//
//		User user = new User();
//		user.setId(Integer.valueOf(id));
//		user.setUserName(userName);
//		user.setGender(Integer.valueOf(gender));
//		try {
//			user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		user.setPhone(phone);
//		user.setAddress(address);
//		user.setUserRole(Integer.valueOf(userRole));
//		user.setModifyBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
//		user.setModifyDate(new Date());
//
//		//UserService userService = new UserServiceImpl();
//		if(userService.modify(user)){
//			response.sendRedirect(request.getContextPath()+"/jsp/user.do?method=query");
//		}else{
//			request.getRequestDispatcher("usermodify.jsp").forward(request, response);
//		}
//
//	}
//
//	private void getUserById(HttpServletRequest request, HttpServletResponse response,String url)
//			throws ServletException, IOException {
//		String id = request.getParameter("uid");
//		if(!StringUtils.isNullOrEmpty(id)){
//			//调用后台方法得到user对象
//			//UserService userService = new UserServiceImpl();
//			User user = userService.getUserById(id);
//			request.setAttribute("user", user);
//			request.getRequestDispatcher(url).forward(request, response);
//		}
//
//	}
//
//	private void delUser(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		String id = request.getParameter("uid");
//		Integer delId = 0;
//		try{
//			delId = Integer.parseInt(id);
//		}catch (Exception e) {
//			// TODO: handle exception
//			delId = 0;
//		}
//		HashMap<String, String> resultMap = new HashMap<String, String>();
//		if(delId <= 0){
//			resultMap.put("delResult", "notexist");
//		}else{
//			//UserService userService = new UserServiceImpl();
//			if(userService.deleteUserById(delId)){
//				resultMap.put("delResult", "true");
//			}else{
//				resultMap.put("delResult", "false");
//			}
//		}
//
//		//把resultMap转换成json对象输出
//		response.setContentType("application/json");
//		PrintWriter outPrintWriter = response.getWriter();
//		outPrintWriter.write(JSONArray.toJSONString(resultMap));
//		outPrintWriter.flush();
//		outPrintWriter.close();
//	}
//
//
//	private void userCodeExist(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		//判断用户账号是否可用
//		String userCode = request.getParameter("userCode");
//
//		HashMap<String, String> resultMap = new HashMap<String, String>();
//		if(StringUtils.isNullOrEmpty(userCode)){
//			//userCode == null || userCode.equals("")
//			resultMap.put("userCode", "exist");
//		}else{
//			//UserService userService = new UserServiceImpl();
//			User user = userService.selectUserCodeExist(userCode);
//			if(null != user){
//				resultMap.put("userCode","exist");
//			}else{
//				resultMap.put("userCode", "notexist");
//			}
//		}
//
//		//把resultMap转为json字符串以json的形式输出
//		//配置上下文的输出类型
//		response.setContentType("application/json");
//		//从response对象中获取往外输出的writer对象
//		PrintWriter outPrintWriter = response.getWriter();
//		//把resultMap转为json字符串 输出
//		outPrintWriter.write(JSONArray.toJSONString(resultMap));
//		outPrintWriter.flush();//刷新
//		outPrintWriter.close();//关闭流
//	}
//
//	private void getRoleList(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		List<Role> roleList = null;
//		RoleService roleService = new RoleServiceImpl();
//		roleList = roleService.getRoleList();
//		//把roleList转换成json对象输出
//		response.setContentType("application/json");
//		PrintWriter outPrintWriter = response.getWriter();
//		outPrintWriter.write(JSONArray.toJSONString(roleList));
//		outPrintWriter.flush();
//		outPrintWriter.close();
//	}
//
//	private void query(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		//查询用户列表
//		String queryUserName = request.getParameter("queryname");
//		String temp = request.getParameter("queryUserRole");
//		String pageIndex = request.getParameter("pageIndex");
//		int queryUserRole = 0;
//		//UserService userService = new UserServiceImpl();
//		List<User> userList = null;
//		//设置页面容量
//    	int pageSize = Constants.pageSize;
//    	//当前页码
//    	int currentPageNo = 1;
//
//		/**
//		 * http://localhost:8090/SMBMS/userlist.do
//		 * ----queryUserName --NULL
//		 * http://localhost:8090/SMBMS/userlist.do?queryname=
//		 * --queryUserName ---""
//		 */
//		System.out.println("queryUserName servlet--------"+queryUserName);
//		System.out.println("queryUserRole servlet--------"+queryUserRole);
//		System.out.println("query pageIndex--------- > " + pageIndex);
//		if(queryUserName == null){
//			queryUserName = "";
//		}
//		if(temp != null && !temp.equals("")){
//			queryUserRole = Integer.parseInt(temp);
//		}
//
//    	if(pageIndex != null){
//    		try{
//    			currentPageNo = Integer.valueOf(pageIndex);
//    		}catch(NumberFormatException e){
//    			response.sendRedirect("error.jsp");
//    		}
//    	}
//    	//总数量（表）
//    	int totalCount	= userService.getUserCount(queryUserName,queryUserRole);
//		System.out.println("数量"+totalCount);
//    	//总页数
//    	PageSupport pages=new PageSupport();
//    	pages.setCurrentPageNo(currentPageNo);
//    	pages.setPageSize(pageSize);
//    	pages.setTotalCount(totalCount);
//
//    	int totalPageCount = pages.getTotalPageCount();
//		System.out.println(totalPageCount);
//    	//控制首页和尾页
//    	if(currentPageNo < 1){
//    		currentPageNo = 1;
//    	}else if(currentPageNo > totalPageCount){
//    		currentPageNo = totalPageCount;
//    	}
//
//		userList = userService.getUserList(queryUserName,queryUserRole,currentPageNo, pageSize);
//		request.setAttribute("userList", userList);
//		List<Role> roleList = null;
//		RoleService roleService = new RoleServiceImpl();
//		roleList = roleService.getRoleList();
//		request.setAttribute("roleList", roleList);
//		request.setAttribute("queryUserName", queryUserName);
//		request.setAttribute("queryUserRole", queryUserRole);
//		request.setAttribute("totalPageCount", totalPageCount);
//		request.setAttribute("totalCount", totalCount);
//		request.setAttribute("currentPageNo", currentPageNo);
//		request.getRequestDispatcher("userlist.jsp").forward(request, response);
//	}
//
//	private void add(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		System.out.println("add()================");
//		String userCode = request.getParameter("userCode");
//		String userName = request.getParameter("userName");
//		String userPassword = request.getParameter("userPassword");
//		String gender = request.getParameter("gender");
//		String birthday = request.getParameter("birthday");
//		String phone = request.getParameter("phone");
//		String address = request.getParameter("address");
//		String userRole = request.getParameter("userRole");
//
//		User user = new User();
//		user.setUserCode(userCode);
//		user.setUserName(userName);
//		user.setUserPassword(userPassword);
//		user.setAddress(address);
//		try {
//			user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		user.setGender(Integer.valueOf(gender));
//		user.setPhone(phone);
//		user.setUserRole(Integer.valueOf(userRole));
//		user.setCreationDate(new Date());
//		user.setCreatedBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
//
//		//UserService userService = new UserServiceImpl();
//		if(userService.add(user)){
//			response.sendRedirect(request.getContextPath()+"/jsp/user.do?method=query");
//		}else{
//			request.getRequestDispatcher("useradd.jsp").forward(request, response);
//		}
//	}
//
//	/**
//	 * Initialization of the servlet. <br>
//	 *
//	 * @throws ServletException if an error occurs
//	 */
//	public void init() throws ServletException {
//		// Put your code here
//	}
//
//}
